<template>
  <div>
    <!--供应商索赔结算 -->
    <TopFilter ref="TopFilterss" :list="topFilterList" :cols="3" :collapse="collapse" labelWidth="120" @filterChange="changeHandle"></TopFilter>
    <FilterTable
      ref="table"
      :columns="columns"
      :params="fetchParams"
      :fetchapi="fetchapi"
      :datakey="'records'"
      :onColumnsChange="(columns) => (this.columns = columns)"
      :onRowSelectChange="selectChange"
      columnsRef="ClaimAccessoryAccountSummary"
    >
      <template slot="controls" slot-scope="props">
        <multiuse-button size="small" type="primary" :click="confirmation.bind()">确认</multiuse-button>
        <multiuse-button size="small" type="primary" :click="exportFileBtn.bind()">导出</multiuse-button>
      </template>
    </FilterTable>
    <BaseDialog :visible.sync="visibleAdd" :title="'编辑'" width="500px" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <AddForm @close="close" :formData="formData"></AddForm>
    </BaseDialog>
        <BaseDialog :visible.sync="suppliers" :title="'供应商选择'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <supplier @close="suppliersClose" @save="suppliersSave" :rowDate="rowDate"></supplier>
    </BaseDialog>
    <BasePrint ref="templateSupplierClaimSettle" :data="settleList" template="templateSupplierClaimSettle" />
  </div>
</template>
<script>
import { getListByPage, updateSettleStatus, supplierSettleExport, printList } from '@/api/claim/factory/SupplierClaimSettle';
import { exportExcelFile } from '@/utils/index';
import { dictionary } from '@/utils/dictMixin';
import AddForm from './AddForm';
import supplier from '@/pages/repair/repairBusiness/supplierInformation/supplier.vue';
export default {
  mixins: [dictionary],
  components: { AddForm, supplier },
  data() {
    return {
      dataList: [],
      rowData: {},
      visible: false,
      detailFlag: false,
      editFlag: false,
      collapse: false,
      infoId: null,
      fetchapi: getListByPage,
      topFilterList: this.topFilterListFun(),
      flterValue: {}, //参数
      columns: this.creatHeader(),
      fetchParams: {}, //ipa 接口
      selectedRows: [],
      visibleAdd: false,
      formData: {},
      settleList: {},
      suppliers: false,
    };
  },
  created() {},
  methods: {
    //搜索数据
    topFilterListFun() {
      return [
        { type: 'INPUT', label: '结算单号', fieldName: 'settleNo', placeholder: '请输入', initialValue: '' },
        {
          type: 'INPUT',
          label: '供应商代码',
          fieldName: 'supplierCode',
          readonly: true,
          placeholder: '请选择供应商代码...',
          rules: [{ required: false, message: '请选择责任商', trigger: 'blur' }],
          unitRender: (p) => {
            return (
              <div>
                <multiuse-button icon="el-icon-search" onClick={() => this.selectResponsibleCode()} />
              </div>
            );
          },
        },
        { type: 'INPUT', label: '供应商名称', readonly: true, fieldName: 'supplierName', placeholder: '', initialValue: '' },
        { type: 'INPUT', label: '扣款月份', fieldName: 'settleMonth', placeholder: '请输入扣款月份例如:202006...', initialValue: '' },
        { type: 'SELECT', label: '结算单状态', fieldName: 'settleStatus', placeholder: '请输入', initialValue: '', itemList: this.createDictList(5599) },
      ];
    },
    //table表头和数据源
    creatHeader() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          fixed: 'left',
          sorter: true,
          render: (props) => {
            return <span>{props.row.index + 1}</span>;
          },
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 100,
          render: (props, h) => {
            return (
              <div style="text-align:center">
                <multiuse-button disabled={props.row.settleStatus == 55991001 ? false : true} size="mini" type="text" onClick={() => this.emitFun(props.row)}>
                  编辑
                </multiuse-button>
                <multiuse-button size="mini" type="text" onClick={() => this.print(props.row)}>
                  打印
                </multiuse-button>
              </div>
            );
          },
        },
        { title: '结算单号', dataIndex: 'settleNo', sorter: true, width: 180 },
        { title: '结算单状态', dataIndex: 'settleStatus', sorter: true, width: 180, dictItems: this.createDictList(5599) },
        { title: '供应商代码', dataIndex: 'supplierCode', sorter: true },
        { title: '供应商名称', dataIndex: 'supplierName', sorter: true },
        { title: '扣款月份', dataIndex: 'settleMonth', sorter: true },
        { title: '索赔单数', dataIndex: 'claimNum', sorter: true },
        { title: '工时费', dataIndex: 'labourAmount', sorter: true, precision: 2 },
        { title: '材料费', dataIndex: 'partAmount', sorter: true, precision: 2 },
        { title: '附加费', dataIndex: 'addAmount', sorter: true, precision: 2 },
        { title: '管理费', dataIndex: 'manageAmount', sorter: true, precision: 2 },
        { title: '外出救援费', dataIndex: 'helpAmount', sorter: true, precision: 2 },
        { title: '其他费用', dataIndex: 'misjudgeAmount', sorter: true, precision: 2 },
        { title: '合计', dataIndex: 'total', sorter: true, precision: 2 },
        { title: '追偿费用', dataIndex: 'settleAmount', sorter: true, precision: 2 },
        { title: '制单人', dataIndex: 'reporter', sorter: true },
        { title: '结算日期', dataIndex: 'settleDate', sorter: true, width: 150 },
        { title: '备注', dataIndex: 'remark', sorter: true },
      ];
    },
    changeHandle(val) {
      this.fetchParams = Object.assign({}, val);
    },
    async exportFileBtn() {
      await supplierSettleExport(this.fetchParams).then((res) => {
        console.log(res);
        if (res.status !== 'failure') {
          exportExcelFile(res.data, '供应商索赔结算信息');
        } else {
          notifyAction('导出失败', 'error');
        }
      });
    },
    async confirmation() {
      console.log(this.selectedRows);
      if (this.selectedRows.length > 0) {
        let str = '';
        this.selectedRows.map((x) => {
          str += x.id + ',';
        });
        await updateSettleStatus({ ids: str}).then((res) => {
          if (res.resultCode === 200) {
            this.$notify.success({ type: '提示信息', message: '操作成功!!' });
            this.close();
          }
        });
        this.fetchParams = { ...this.fetchParams };
      } else {
        return this.$notify.warning({ title: '提示信息', message: '请勾选后再进行确认！' });
      }
    },
    //选中
    selectChange(rows) {
      this.selectedRows = rows;
    },
    emitFun(val) {
      this.visibleAdd = true;
      this.formData = val;
    },
    close() {
      this.visibleAdd = false;
      this.fetchParams = Object.assign({}, this.fetchParams);
    },
    async print(row) {
      var res = await printList(row);
      if (res.resultCode === 200) {
        this.settleList.rows = res.data;
        this.$nextTick(() => {
          this.$refs.templateSupplierClaimSettle.EXCUTE_PRINT();
        });
      }
    },
    //供应商选择
    selectResponsibleCode() {
      this.suppliers = true;
    },
    suppliersSave(obj) {
      this.topFilterList.find((x) => x.fieldName == 'supplierName').initialValue = obj[0].supplierName;
      this.topFilterList.find((x) => x.fieldName == 'supplierCode').initialValue = obj[0].supplierCode;
      this.suppliers = false;
    },
    suppliersClose() {
      this.suppliers = false;
    },
  },
};
</script>
<style lang="less" scoped></style>
